package com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.XjrUser;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseUserRelationService;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.entity.TBarinUser;
import com.xjrsoft.module.customerTwo.barin.t_barin_user_registration.service.ITBarinUserService;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.dto.TBrainSubCcountDto;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.dto.SaveTBrainSubCcountFormDataDto;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.vo.TBrainSubCcountVo;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.vo.TBrainSubCcountListVo;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.dto.TBrainSubCcountListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.entity.TBrainSubCcount;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.vo.TBrainSubCcountVo;
import com.xjrsoft.module.customerTwo.barin.t_brain_sub_ccount.service.ITBrainSubCcountService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-05-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_brain_sub_ccount")
@Api(value = "子账号管理", tags = "子账号管理接口")
public class TBrainSubCcountController {

	private final ITBarinUserService xjrBaseUserService;
	private final ITBrainSubCcountService tBrainSubCcountService;
	private final IXjrBaseUserRelationService xjrBaseUserRelationService;
	private static String ROLE_ADMIN="1956502f09d577e3d1ab5bede35c6869";	//用户角色:子账号-管理员
	private static String ROLE_AFTER_SALE_SERVICE="acea615104ae2bff4259233b06b2f801";	//用户角色:子账号-售后服务



	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tBrainSubCcount")
	@SaCheckPermission(value = {"t_brain_sub_ccount:view", "t_brain_sub_ccount:modify"}, mode = SaMode.OR)
	public Response<TBrainSubCcountVo> getFormData(@PathVariable String id) {
		// 主表数据
		TBrainSubCcount tBrainSubCcount = tBrainSubCcountService.getById(id);
		TBrainSubCcountVo tBrainSubCcountVo = BeanUtil.copy(tBrainSubCcount, TBrainSubCcountVo.class);
		return Response.ok(tBrainSubCcountVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tBrainSubCcount")
		public Response<PageOutput<TBrainSubCcountListVo>> getPageList(TBrainSubCcountListDto listDto) {
		//如果是管理员登录，可以查询所有数据，如果是商户登录，只能看到自己账号下的子账号数据
		IPage<TBrainSubCcount> page = tBrainSubCcountService.getPageList(listDto);
		XjrUser currentUser = SecureUtil.getUser();
		if(!currentUser.isAdminUser()){
			listDto.setF_MerchantId(currentUser.getMerchantid());
		}
		List<TBrainSubCcountListVo> records = BeanUtil.copyList(page.getRecords(), TBrainSubCcountListVo.class);
			// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tBrainSubCcount")
	@SaCheckPermission("t_brain_sub_ccount:add")
	public Response save(@RequestBody SaveTBrainSubCcountFormDataDto formDto) {
		XjrUser currentUser = SecureUtil.getUser();
		TBrainSubCcountDto tBrainSubCcountDto = formDto.getTBrainSubCcountDto();
		TBrainSubCcount tBrainSubCcount = BeanUtil.copy(tBrainSubCcountDto, TBrainSubCcount.class);
		tBrainSubCcount.setFMerchantid(currentUser.getMerchantid());
		TBarinUser user = xjrBaseUserService.getUserByAccountAndTelephone(tBrainSubCcount.getFAccount(),tBrainSubCcount.getFTelephone());
		if(user == null){
			throw new RuntimeException("当前账号不存在，请核对账号与手机号后重试。");
		}else if(!"2".equals(user.getFUsertype())){
			String userType = "1".equals(user.getFUsertype())?"服务商":"企业用户";
			throw new RuntimeException(userType+"不能被添加为子账号，请检查后重试。");
		}
		tBrainSubCcount.setFUserid(user.getFUserid());
		user.setFMerchantid(tBrainSubCcount.getFMerchantid());
		user.setFSubaccountrole(tBrainSubCcount.getFSubaccountrole());
		xjrBaseUserService.updateXjrBaseUser(user.getFUserid(),user);
		//给用户添加角色
		List<String> roleList = new ArrayList<>();
		roleList.add("1".equals(tBrainSubCcount.getFSubaccountrole())?ROLE_ADMIN:ROLE_AFTER_SALE_SERVICE);
		xjrBaseUserRelationService.addUserRelationsForUser(user.getFUserid(),1,roleList,true);
		String dataId = tBrainSubCcountService.addTBrainSubCcount(tBrainSubCcount);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tBrainSubCcount")
	@SaCheckPermission("t_brain_sub_ccount:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTBrainSubCcountFormDataDto formDto) {
		TBrainSubCcountDto tBrainSubCcountDto = formDto.getTBrainSubCcountDto();
		TBrainSubCcount tBrainSubCcount = BeanUtil.copy(tBrainSubCcountDto, TBrainSubCcount.class);
		TBarinUser user = xjrBaseUserService.getById(tBrainSubCcount.getFUserid());
		//修改用户角色
		List<String> roleList = new ArrayList<>();
		roleList.add("1".equals(tBrainSubCcount.getFSubaccountrole())?ROLE_ADMIN:ROLE_AFTER_SALE_SERVICE);
		xjrBaseUserRelationService.addUserRelationsForUser(user.getFUserid(),1,roleList,true);
		return Response.status(tBrainSubCcountService.updateTBrainSubCcount(id, tBrainSubCcount));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_brain_sub_ccount:batchDelete", "t_brain_sub_ccount:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tBrainSubCcountService.delete(ids));
	}


}
